package cn.wubo.jdbc.dao.impl;

import cn.wubo.jdbc.dao.IDemoTableDao;
import cn.wubo.jdbc.entity.DemoTable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DemoTableDaoImpl implements IDemoTableDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(DemoTable demoTable) {
        return jdbcTemplate.update("insert into demo_table(name,message) values (?,?)",demoTable.getName(),demoTable.getMessage());
    }

    @Override
    public int update(DemoTable demoTable) {
        return jdbcTemplate.update("update demo_table set name = ? , message = ?  where id = ?",demoTable.getName(),demoTable.getMessage(),demoTable.getId());
    }

    @Override
    public int delete(int id) {
        return jdbcTemplate.update("delete from demo_table account where id = ?",id);
    }

    @Override
    public DemoTable getById(int id) {
        List<DemoTable> list = jdbcTemplate.query("select * from demo_table where id = ?", new Object[]{id}, new BeanPropertyRowMapper(DemoTable.class));
        if(list!=null && list.size()>0){
            DemoTable demoTable = list.get(0);
            return demoTable;
        }else{
            return null;
        }
    }

    @Override
    public List<DemoTable> getAllList() {
        List<DemoTable> list = jdbcTemplate.query("select * from demo_table", new Object[]{}, new BeanPropertyRowMapper(DemoTable.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }
    }
}
